package com.xzy.shop.dao.impl;

import com.xzy.shop.dao.CategoryDao;
import com.xzy.shop.domain.Category;
import com.xzy.shop.domain.Goods;
import com.xzy.shop.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CategoryDaoImpl implements CategoryDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Category> findAll() {
        String sql = "select * from `category`";
        return template.query(sql, new BeanPropertyRowMapper<Category>(Category.class));
    }

    @Override
    public Category findCategory(String gid) {
        String sql = "select distinct cate.cid, cate.name from category cate, goods gs where cate.cid = (select cid from goods where gid = ?) ";
        return template.queryForObject(sql, new BeanPropertyRowMapper<Category>(Category.class), gid);
    }

    @Override
    public String findNameByCid(String cid) {
        String sql = "select `name` from category where cid = ?";
        String s = template.queryForObject(sql, String.class, cid);
        return s;
    }
}
